---
title: ""
output: pdf_document
geometry: "top=1in, bottom=1in, left=1in, right=1in"
header-includes:
    - \usepackage{caption}
    - \usepackage{lscape}
---
\newcommand{\blandscape}{\begin{landscape}}
\newcommand{\elandscape}{\end{landscape}}

\blandscape

```{r setup, include=FALSE, message=FALSE}
# Package setup
if (!require("pacman")) install.packages("pacman", repos = "http://cran.us.r-project.org"); library(pacman)
pacman::p_load(here, tibble, magrittr, stringr, dplyr, knitr, kableExtra, glue, tidyr, data.table, readr, yaml)

# Working directory and command line argument setup
current_script <- "generate_tables_and_figures/gen_prog_component_importance_table/src/gen_prog_component_importance_table.Rmd"
here::i_am(current_script)
source(here("R", "project_functions.R"))
task_dir <- here(dirname(dirname(file.path(current_script))))
knitr::opts_knit$set(root.dir = task_dir)
cl_args <- parse_make_args(c(
  "CONFIG_RAW_DATA",
  "OUT_DIR",
  "TABLE_CONFIG"), inter_active = TRUE, task_dir = task_dir)

knitr_setup()
```


```{r setup2, include=FALSE, message=FALSE}
# For transposing data so that you can perform operations on a row (by temporarily treating it as a column)
transpose_and_clean <- function(dframe){
  dframe %>% 
    transpose(keep.names = "column_titles") %>% 
    # move column names from row back to column name
    set_colnames(.[1,]) %>% 
    # delete column-name row
    slice(-1) %>% 
    # return to tibble
    as_tibble()
}
```


```{r}
tables_config <- read_yaml(cl_args$TABLE_CONFIG)

# Values copied directly from https://docs.google.com/document/d/13pHGM-8zXMIjrz_jnkaVqYUDhiIwkfm9p2wtG5jkaWU/edit?pli=1#heading=h.bnm69m39n1vm
# Data doesn't seem to be easily writeable in a csv format so I hardcoded it into a csv file defined in hand/
# Except for last row, which is raw values, all data are percentages
results <- read_csv(cl_args$CONFIG_RAW_DATA) %>% 
  mutate(across(All:Managers, ~ as.character(round(as.numeric(.x), 1)))) %>% 
  mutate(across(All:Managers, ~ if_else(.x == "20", "20.0", .x)))

results_transposed <- transpose_and_clean(results) %>% 
  # Create N responded row
  mutate(`N responded` = as.character(round((as.numeric(`Percent Responded`)/100)*as.numeric(`N Total`), 1)))

results <- results_transposed %>%
  mutate("Responded to Question" = paste0(`N responded`, "/", `N Total`)) %>% 
  select(Categories:`Some Combination`, `Responded to Question`) %>% 
  transpose_and_clean()

headers <- c("", colnames(results)[colnames(results) != "Categories"])

(table <- results %>%
    kable(booktabs = tables_config$booktabs, longtable = tables_config$longtable,
          col.names = headers,
          align = c(tables_config$alignment$text, rep(tables_config$alignment$numeric, 5)),
          linesep = '') %>% 
    kable_styling(font_size = tables_config$font_size) %>%
    column_spec(1, width="16em", latex_valign = "m") %>%
    column_spec(2:6, width="7em", latex_valign = "m") %>%
    row_spec(nrow(results)-1, hline_after = T))

# SAVE
timestamp <- get_timestamp()
writeLines(table, glue(cl_args$OUT_DIR, "/prog_component_importance_up_to_date.tex"))
writeLines(table, glue(cl_args$OUT_DIR, "/prog_component_importance_{timestamp}.tex"))

```

\elandscape
